const dateFormat = (date : Date, format = 'yyyy-MM-dd hh:mm:ss') => {
	const o = {
		'M+': date.getMonth() + 1,
		'd+': date.getDate(),
		'h+': date.getHours(),
		'm+': date.getMinutes(),
		's+': date.getSeconds(),
		'q+': Math.floor((date.getMonth() + 3) / 3),
		'S': date.getMilliseconds(),
	}
	if (/(y+)/.test(format)) {
		format = format.replace(
			RegExp.$1,
			(`${date.getFullYear()}`).substr(4 - RegExp.$1.length),
		)
	}
	for (const k in o) {
		if (new RegExp(`(${k})`).test(format)) {
			format = format.replace(
				RegExp.$1,
				// @ts-expect-error ...
				RegExp.$1.length === 1 ? o[k] : (`00${o[k]}`).substr((`${o[k]}`).length),
			)
		}
	}
	return format
}

const queryString = (key : string) => {
	const url = location.search || location.hash
	const re = new RegExp(`${key}=([^&?#/]*)`, 'ig')
	return url.match(re) ? decodeURI((url as any).match(re)[0].substr(key.length + 1)) : ''
}

const pad = (n : number) => {
	return n < 10 ? '0' + n : n;
}
const getFullTime = (timestamp : number, type : string = "time") => {
	var date = new Date(timestamp * 1000);
	var year = date.getFullYear();
	var month = pad(date.getMonth() + 1); 
	var day = pad(date.getDate());
	var hour = pad(date.getHours());
	var minute = pad(date.getMinutes()); 
	var second = pad(date.getSeconds());
	
	var nowDate = new Date();
	var nowYear = nowDate.getFullYear();
	var nowMonth = pad(nowDate.getMonth() + 1); 
	var nowDay = pad(nowDate.getDate());
	var str = month + "-" + day + ", "
	if(nowYear==year&&nowMonth==month&&nowDay==day){
		str = "今天"
	}
	if (type == "time") {
		return  str + hour + ":" + minute;
	} else if (type == "day") {
		return month + "-" + day;
	}
}
const getImg = (url: string) => {
    return new URL(`../assets/image/${url}`, import.meta.url).href;
};

export default {
	dateFormat,
	queryString,
	getFullTime,
	getImg
}